package com.lm.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.lm.stream.StreamTest;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 19:45 2024/5/9
 */
public class NormalJsonTest {
    private static void test() {
        JSONObject object = new JSONObject();
        //string
        object.put("string", "string");
        //int
        object.put("int", 2);
        //boolean
        object.put("boolean", true);
        //array
        List<Integer> integers = Arrays.asList(1, 2, 3);
        object.put("list", integers);
        //null
        object.put("null", null);
        System.out.println(object);
    }


    public static void main(String[] args) throws Exception {
        testJson2();
    }

    public static void testJson2() throws IOException {
        StreamTest.Org org = new StreamTest.Org(8, "s5", LocalDate.of(2020, 11, 13), 1000000d, "2");
        System.out.println(JSON.toJSONString(org));
        String listString = "[{\"amt\":100000.0,\"date\":\"2022-07-22\",\"id\":5,\"num\":\"s3\",\"status\":\"1\"},{\"amt\":500000.0,\"date\":\"2023-09-15\",\"id\":6,\"num\":\"s3\",\"status\":\"1\"},{\"amt\":20000.0,\"date\":\"2020-11-11\",\"id\":8,\"num\":\"s4\",\"status\":\"1\"},{\"amt\":1000000.0,\"date\":\"2021-11-13\",\"id\":8,\"num\":\"s5\",\"status\":\"1\"},{\"amt\":200000.0,\"date\":\"2021-03-04\",\"id\":2,\"num\":\"s1\",\"status\":\"1\"}]";
        System.out.println(listString);
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());//添加这个modeule 反序列时才能解析LocalDate
        StreamTest.Org org1 = mapper.readValue(JSON.toJSONString(org), StreamTest.Org.class);
        System.out.println(org1);
        List<StreamTest.Org> list= mapper.readValue(listString, new TypeReference<List<StreamTest.Org>>() {
        });
        System.out.println(list);
    }

}
